// pages/groupStatus/groupStatus.js
const app = getApp();
var utils = require("../../utils/util.js");
var noLogin = require("../template/noLogin.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //遮罩层显示状态
    mask: true,
    //承诺保障弹窗
    promiseBox: true,
    isStatus: 1,  //1:拼团中，2:拼团成功，3：拼团失败
    hasUserInfo: true
  },
  onLoad: function (options){
    
  },
  onShow: function(){
    if (!wx.getStorageSync('userInfo')) {
      noLogin.showNoLogin(this).then(() => {
        this.init(); //用户授权后更新用户数据
      }).catch(() => {
        this.setData({ hasUserInfo: false })
      });  //判断用户未登录，弹出授权窗口
    } else {
      this.setData({ hasUserInfo: true }); //判断用户已登录，关闭授权窗口
      this.init();
    }
  },
  //获取数据
  init(){
    wx.request({
      url: app.baseURL + 'group&a=group_detail',
      data: {
        user_id: app.globalData.userId,
        pid: this.options.pid
      },
      success: (res) => {
        if (res.statusCode == 200) {
          const data = res.data.info,
            ptstatus = data.re.ptstatus,  //拼团状态
            isUserGroup = data.re.status;  //判断是否参团
          var index = 1;
          if (ptstatus == 1 && isUserGroup == 1) {
            // 拼团中且当前用户已参团  1--->邀请好友参团
            index = 1
          }
          if (ptstatus == 1 && isUserGroup == 0) {
            // 拼团中且当前用户未参团  2--->参与活动
            index = 2
          }
          if (ptstatus == 2 && isUserGroup == 1) {
            // 拼团完成且当前用户已参团  3--->继续逛逛
            index = 3
          }
          if (ptstatus == 2 && isUserGroup == 0) {
            // 拼团完成且当前用户未参团  4--->我来开这个团
            index = 4
          }
          if (ptstatus == 3) {
            // 拼团失败  5--->活动已结束
            index = 5
          };
          if (index == 1 || index == 2) {
            clearInterval(this.countFun); // //清除定时器避免重复
            this.countFun = setInterval(() => {
              data.re.time--;  //时间自减
              data.re.time < 0 && clearInterval(this.countFun);  // 时间到了清除定时器
              this.setData({ ['thatData.re.time']: utils.seckill(data.re.time) });
            }, 1000);
          }
          this.setData({
            thatData: data,
            ['thatData.re.time']: utils.seckill(data.re.time),
            isStatus: index
          });
          // 如果有回调执行回调
          typeof cb == 'function' && cb();
        }
      }
    });
  },

  //用户确认授权
  WeChatUserInfo: function (e) {
    if (e.detail.userInfo) {
      this.setData({ hasUserInfo: true });
      //发起登录
      //用户授权后更新用户数据
      noLogin.getUserInfo(this).then(() => {
        this.init();
      });
    } else {
      wx.showModal({
        title: '温馨提示',
        content: '取消微信授权将无法使用部分功能',
        success: res => {
          if (res.confirm) {
            // wx.setStorageSync("rejectLogin", true);  //用户拒绝登录添加本地缓存状态
            wx.switchTab({
              url: '/pages/index/index'
            })
          }
        }
      })
    }
  },

  //参团按钮
  joinGroup(e){
    const status = e.currentTarget.dataset.status,
    data = this.data.thatData;
    if (status == 0){
      wx.navigateTo({
        url: '/pages/groupDetail/groupDetail?item_id=' + data.info.item_id + '&a_id=' + data.info.id + '&pid=' + data.re.id,
      })
    }
    if (status == 1) {
      wx.navigateTo({
        url: '/pages/groupDetail/groupDetail?item_id=' + data.info.item_id + '&a_id=' + data.info.id,
      })
    }
  },

  //点击 跳转详情页
  clickToDetail(e) {
    const itemId = e.currentTarget.dataset.item_id,
    a_id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/groupDetail/groupDetail?item_id=' + itemId + '&a_id=' + a_id
    })
  },
  //显示保障弹窗
  showPromiseBox() {
    this.setData({
      promiseBox: !this.data.promiseBox,
      mask: !this.data.mask
    })
  },
  //点击遮罩层隐藏弹窗
  hideAllBox() {
    this.setData({
      //遮罩层隐藏
      mask: true,
      //承诺保障弹窗隐藏
      promiseBox: true,
      //产品参数弹窗隐藏
      paramsBox: true,
      //购物车弹窗隐藏
      cartBox: true,
    })
  },

  // 转发
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
    }
    return {
      title: '拼团',
      path: '/pages/groupStatus/groupStatus?pid=' + this.options.pid
    }
  },
  // 下拉刷新
  onPullDownRefresh() {
    this.init(wx.stopPullDownRefresh());
  },
})